<?php



class CategoryList {




	public $CI;

	public function __construct() {
		// get main CI object
		$this->CI = & get_instance();
		if(CI_VERSION >= 2.2){
			$this->CI->load->library('driver');
		}
		$this->CI->load->database();
	}


	function level ($id = false,$parent_id = false)
	{
		$cats = $this->allCate();
		if($parent_id){
			return $this->renderEdit($cats, $id ,$parent_id);
		}else{
			return $this->render($cats);
		}
	}


	private function render(&$parent)
	{
		$data = array();
		foreach ($parent as $p){
			if($p->children){
				$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).'><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->render($p->children).'</li></ul>';
			}else{
				$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).'><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
			}
		}

		return implode ($data);
	}


	private function renderEdit(&$parent,$id = false,$parent_id = false,$disabled = false,$this_id = false)
	{
		$data = array();

		foreach ($parent as $p){
			if($disabled==true){
				if($p->children){
					if($parent_id == $p->id){
						$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked" disabled><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = false).'</li></ul>';
					}else{
						if($id == $p->parent_id){
							$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' disabled><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = true, $p->id).'</li></ul>';
						}else{
							$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' ><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = false).'</li></ul>';
						}
					}
				}else{

					if($parent_id == $p->id){
						$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked" ><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
					}else{
						if($id == $p->parent_id || $this_id == $p->parent_id ){
							$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).'  disabled><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
						}else{
							$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' ><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
						}
					}
				}
			}else{
				if($p->children){
					if($parent_id == $p->id){
						$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked" ><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = false).'</li></ul>';
					}else{
						if($id == $p->id){
							$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' disabled><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = true).'</li></ul>';
						}else{
							$data[] =  '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' ><i class="fa fa-folder-open"></i> '.$p->name_cn.'</label></div>'.$this->renderEdit($p->children,$id,$parent_id,$disabled = false).'</li></ul>';
						}
					}
				}else{

					if($parent_id == $p->id){
						$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked" ><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
					}else{
						if($id == $p->id){
							$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).'  disabled><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
						}else{
							$data[] = '<ul><li><div class="radio"><label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' ><i class="fa fa-angle-right"></i> '.$p->name_cn.'</label></div></li></ul>';
						}
					}

				}
			}
		}

		return implode ($data);
	}


	function getlist ($cat_id = false)
	{
		$cats = $this->allCate();
		return $this->makeList($cats,$cat_id);
	}

	private function makeList(&$parent,$cat_id)
	{
		$data = array();
		foreach ($parent as $p){
			if($p->children){
				if($p->id == $cat_id){
					$data[] =  '<ul><li><div class="radio"><i class="fa fa-minus-square"></i> <a href="'.site_url().'products/view/'.$p->id.'" class="btn btn-xs btn-primary" disabled> '.$p->name_cn.' [ 第'.$p->level_depth.'级 ]</a> <i class="fa fa-arrow-left text-success"></i></div>'.$this->makeList($p->children, $cat_id).'</li></ul>';
				}else{
					$data[] =  '<ul><li><div class="radio"><i class="fa fa-minus-square"></i> <a href="'.site_url().'products/view/'.$p->id.'" class="btn btn-xs btn-primary"> '.$p->name_cn.' [ 第'.$p->level_depth.'级 ]</a></div>'.$this->makeList($p->children, $cat_id).'</li></ul>';
				}
			}else{
				if($p->id == $cat_id){
					$data[] = '<ul><li><div class="radio"><i class="fa fa-folder-open-o"></i> <a href="'.site_url().'products/view/'.$p->id.'" class="btn btn-xs btn-primary" disabled> '.$p->name_cn.' [ 第'.$p->level_depth.'级 ]</a> '.'<i class="fa fa-arrow-left text-success"></i></div></li></ul>';
				}else{
					$data[] = '<ul><li><div class="radio"><i class="fa fa-folder-open-o"></i> <a href="'.site_url().'products/view/'.$p->id.'" class="btn btn-xs btn-primary"> '.$p->name_cn.' [ 第'.$p->level_depth.'级 ]</a> '.'</div></li></ul>';
				}
			}
		}

		return implode ($data);
	}


	function getlistEdit ($cat_id = false)
	{
		$cats = $this->allCate();
		return $this->makeListEdit($cats,$cat_id);
	}

	private function makeListEdit(&$parent,$cat_id)
	{
		$data = array();
		foreach ($parent as $p){
			if($p->children){
				if($p->id == $cat_id){
					$data[] =  '<ul><li><div class="radio"><i class="fa fa-minus-square"></i> <label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked">'.$p->name_cn.' [ 第'.$p->level_depth.'级 ] <span class="text-success"><i class="fa fa-arrow-left"></i> 当前分类</span></label></div>'.$this->makeListEdit($p->children, $cat_id).'</li></ul>';
				}else{
					$data[] =  '<ul><li><div class="radio"><i class="fa fa-minus-square"></i> <label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' >'.$p->name_cn.' [ 第'.$p->level_depth.'级 ]</label></div>'.$this->makeListEdit($p->children, $cat_id).'</li></ul>';
				}
			}else{
				if($p->id == $cat_id){
					$data[] = '<ul><li><div class="radio"><i class="fa fa-folder-open-o"></i> <label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' checked="checked">'.$p->name_cn.' [ 第'.$p->level_depth.'级 ] '.'<span class="text-success"><i class="fa fa-arrow-left"></i> 当前分类</span></label></div></li></ul>';
				}else{
					$data[] = '<ul><li><div class="radio"><i class="fa fa-folder-open-o"></i> <label><input type="radio" name="cat_id"  value="'.$p->id.'" '.set_radio("cat_id", $p->id).' >'.$p->name_cn.' [ 第'.$p->level_depth.'级 ] '.'</label></div></li></ul>';
				}
			}
		}

		return implode ($data);
	}

	private function allCate()
	{
		$top = category::get_topLevel();

		$new = array();

		foreach ($top as $t){
			if($t->children){
				$this->tree($t->children);
			}
			$new[]  = $t;
		}
		return $new;
	}

	private function tree(&$parent)
	{
		$new = array();
		foreach ($parent as $p){
			if($p->children){
				$this->tree($p->children);
			}
			$new[]  = $p;
		}

		return $new;
	}


}